package com.proto3.proto;

import com.google.protobuf.InvalidProtocolBufferException;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Map;

public class TestRead {
    public static void main(String[] args) throws IOException {
        //读取通讯录-->反序列化--->打印

        Contacts contacts = Contacts.parseFrom(new FileInputStream("src/main/java/com/proto3/proto/contacts.bin"));
//        printContacts(contacts);
        System.out.println(contacts.toString());

    }

    private static void printContacts(Contacts contacts) throws InvalidProtocolBufferException {
        int i = 0;
        for (PersonInfo personInfo : contacts.getPersonsList()) {
            System.out.println("-----联系人" + i++ + "-----");
            System.out.println("姓名:" + personInfo.getName());
            System.out.println("年龄:" + personInfo.getAge());
            switch (personInfo.getOthercontactCase()) {
                case QQ:
                    System.out.println("QQ:" + personInfo.getQq());
                    break;
                case WECHAT:
                    System.out.println("微信" + personInfo.getWechat());
                    break;
                case OTHERCONTACT_NOT_SET:
                    break;
            }
            int j = 0;
            for (PersonInfo.Phone phone : personInfo.getPhonesList()) {
                System.out.println("电话号码" + j++ + ":" + phone.getNumber() + "  类型:" + phone.getType().name());
            }
            if (personInfo.hasAddress() && personInfo.getAddress().is(Address.class)) {
                Address address = personInfo.getAddress().unpack(Address.class);
                System.out.println("家庭地址: " + address.getHomeAddress());
                System.out.println("公司地址: " + address.getCompanyAddress());
            }
            //打印备注信息
            for (Map.Entry<String, String> entry : personInfo.getRemarkMap().entrySet()) {
                System.out.println("备注:" + entry.getKey() + ":" + entry.getValue());
            }

        }
    }

}
